package com.cjlgb.design.common.core.util;

import com.cjlgb.design.common.core.constant.HttpConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author WFT
 * @date 2019/5/18
 * description: Web工具类
 */
@SuppressWarnings(value = "all")
public class WebUtils {

    /**
     * 获取HttpServletRequest对象
     * @return request
     */
    public static HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取Ip
     * @return Ip
     */
    public static String getIp(){
        HttpServletRequest request = getHttpServletRequest();
        //  获取远程地址
        String ip = request.getRemoteAddr();
        for (String ipHeader : HttpConstant.IP_HEADER_ARRAY) {
            String value = request.getHeader(ipHeader);
            if (!StringUtils.isEmpty(value) && !value.equalsIgnoreCase(HttpConstant.UNKNOWN)){
                ip = value;
                break;
            }
        }
        return StringUtils.isEmpty(ip) ? null : ip.split(",")[0];
    }


    /**
     * 获取 HttpServletResponse
     * @return HttpServletResponse
     */
    public static HttpServletResponse getHttpServletResponse(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 根据名称,获取cookie内容
     * @param cookieName cookie名称
     * @return String
     */
    public static String getCookie(String cookieName){
        HttpServletRequest request = getHttpServletRequest();
        return getCookie(cookieName,request);
    }

    /**
     * 根据名称,获取cookie内容
     * @param cookieName cookie名称
     * @param request HttpServletRequest
     * @return String
     */
    public static String getCookie(String cookieName,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (null == cookies){
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())){
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 设置cookie
     * @param cookieName 名称
     * @param content 内容
     */
    public static void setCookie(String cookieName,String content){
        setCookie(cookieName,content,-1);
    }

    /**
     * 设置cookie
     * @param cookieName 名称
     * @param content 内容
     * @param maxAge 有效期
     */
    public static void setCookie(String cookieName,String content,int maxAge){
        HttpServletRequest request = getHttpServletRequest();

        Cookie cookie = new Cookie(cookieName,content);
        //  设置域名
//        cookie.setDomain(getDomain(request));
        //  设置作用域
        cookie.setPath(request.getServletContext().getContextPath() + "/");
        //  设置有效期
        cookie.setMaxAge(maxAge);

        //  向客户端返回cookie
        HttpServletResponse response = getHttpServletResponse();
        response.addCookie(cookie);
    }

    /**
     * 从request中获取域名
     * @param request HttpServletRequest
     * @return String
     */
    private static String getDomain(HttpServletRequest request){
        //  从请求路径中得到域名
        StringBuffer requestURL = request.getRequestURL();
        //  得到 // 的下标
        int index = requestURL.indexOf("//");
        //  删除http://或https://
        requestURL.delete(0,index + 2);
        //  判断路径中是否含有端口
        index = requestURL.indexOf(":");
        if (index != -1){
            //  删除端口及后面的所有内容
            requestURL.delete(index,requestURL.length());
        }else {
            //  删除URI
            requestURL.delete(requestURL.length() - request.getRequestURI().length(), requestURL.length());
        }
        return requestURL.toString();
    }

}
